package com.xbts.web.controller.cloud;

import com.alibaba.fastjson2.JSONObject;
import com.huaweicloud.sdk.bss.v2.model.*;
import com.huaweicloud.sdk.ecs.v2.model.DeleteServersRequest;
import com.huaweicloud.sdk.ecs.v2.model.DeleteServersResponse;
import com.huaweicloud.sdk.ecs.v2.model.NovaListAvailabilityZonesResponse;
import com.huaweicloud.sdk.ecs.v2.model.ShowJobResponse;
import com.huaweicloud.sdk.eip.v2.model.ListPublicipsRequest;
import com.huaweicloud.sdk.iam.v3.model.KeystoneListProjectsResponse;
import com.huaweicloud.sdk.ims.v2.model.CreateImageResponse;
import com.huaweicloud.sdk.ims.v2.model.ListImagesResponse;
import com.huaweicloud.sdk.vpc.v2.model.CreateSubnetRequest;
import com.huaweicloud.sdk.vpc.v2.model.CreateSubnetResponse;
import com.huaweicloud.sdk.vpc.v2.model.ListSubnetsResponse;
import com.huaweicloud.sdk.vpc.v2.model.ListVpcsResponse;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.system.domain.dto.*;
import com.xbts.system.service.ICloudServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cloud")
public class CloudServiceController extends BaseController {

    @Autowired
    private ICloudServiceService cloudServiceService;

    /**
     * 查询区域列表
     * @return 区域列表
     */
    @GetMapping("/regionList")
    public AjaxResult regionList() {
        return success(cloudServiceService.regionList());
    }


    /**
     * https://support.huaweicloud.com/api-ecs/ecs_03_1801.html
     * 查询可用区列表
     * @param regionId 项目id
     * @return 可用区列表
     */
    @GetMapping("/novaListAvailabilityZones")
    public AjaxResult novaListAvailabilityZones(String regionId) {
        NovaListAvailabilityZonesResponse novaListAvailabilityZonesResponse = cloudServiceService.novaListAvailabilityZones(regionId);
        if (novaListAvailabilityZonesResponse == null){
            return error("查询可用区列表失败");
        }
        return success(novaListAvailabilityZonesResponse);
    }
    /**
     * https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212656.html
     * 查询云服务器规格列表
     * @param listFlavorsRequestDTO 请求参数
     * @return
     */
    @PostMapping("/listFlavors")
    // #result == null 表示如果返回结果为null，则不缓存
//    @Cacheable(value = "listFlavors", unless = "#result == null")
    public AjaxResult listFlavors(@RequestBody ListFlavorsRequestDTO listFlavorsRequestDTO) {
        return success(cloudServiceService.listFlavors(listFlavorsRequestDTO));
    }

    /**
     * https://support.huaweicloud.com/api-oce/bcloud_01002.html
     * 查询包年/包月产品价格
     * @param rateOnPeriodReq 请求参数
     * @return
     */
    @RequestMapping("/listRateOnPeriodDetail")
    public AjaxResult listRateOnPeriodDetail(@RequestBody RateOnPeriodReqDTO rateOnPeriodReq) {
        Map<String, Object> map = cloudServiceService.listRateOnPeriodDetail(rateOnPeriodReq);
        return success(map);
    }

    /**
     * 变更差价查询
     * @param rateOnPeriodReq
     * @return
     */
    @RequestMapping("/resizeFlavorPrice")
    public AjaxResult resizeFlavorPrice(@RequestBody RateOnPeriodReqDTO rateOnPeriodReq) {
        Map<String, Object> map = cloudServiceService.resizeFlavorPrice(rateOnPeriodReq);
        if (map == null){
            return error("查询包年/包月产品价格失败");
        }
        return success(map);
    }

    /**
     * https://support.huaweicloud.com/api-ims/ims_03_0702.html
     * 查询镜像列表
     * @param listImagesRequest 请求参数
     * @return
     */
    @RequestMapping(value = "/glanceListImages", method = RequestMethod.POST)
    public AjaxResult glanceListImages(@RequestBody ListImagesRequestDTO listImagesRequest) {
        ListImagesResponse listImagesResponse = cloudServiceService.glanceListImages(listImagesRequest);
        if (listImagesResponse == null){
            return error("查询镜像列表失败");
        }
        return success(listImagesResponse);
    }

    /**
     * https://support.huaweicloud.com/api-ims/ims_03_0702.html
     * 制作镜像
     * @param createImageRequestDTO 制作镜像
     * @return
     */
    @RequestMapping(value = "/createImage", method = RequestMethod.POST)
    public AjaxResult createImage(@RequestBody CreateImageRequestDTO createImageRequestDTO) {
        CreateImageResponse response = cloudServiceService.createImage(createImageRequestDTO);
        if (response == null){
            return error("制作镜像失败");
        }
        return success(response);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_03_0401.html
     * 查询网络列表
     * @param regionId 请求参数
     * @return
     */
    @GetMapping(value = "/networks/{regionId}")
    public AjaxResult networks(@PathVariable("regionId") String regionId) {
        JSONObject networks = cloudServiceService.networks(regionId);
        if (networks == null){
            return error("查询网络列表失败");
        }
        return success(networks);
    }

    /**
     * https://support.huaweicloud.com/api-vpc/vpc_api01_0003.html
     * 查询VPC列表
     * @param regionId 请求参数
     * @return
     */
    @GetMapping(value = "/listVpcs")
    public AjaxResult listVpcs(String regionId) {
        ListVpcsResponse listVpcsResponse = cloudServiceService.listVpcs(regionId);
        if (listVpcsResponse == null){
            return error("查询VPC列表失败");
        }
        return success(listVpcsResponse);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0101.html
     * 创建云服务器
     * @param prePaidServerDTO 请求参数
     * @return
     */
    @PostMapping(value = "/createServers")
    public AjaxResult createServers(@RequestBody PrePaidServerDTO prePaidServerDTO) {
        return cloudServiceService.createServers(prePaidServerDTO);
    }


    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0101.html
     * 查询子网列表
     * @param listSubnetsRequestDTO 请求参数
     * @return
     */
    @PostMapping(value = "/listSubnets")
    public AjaxResult listSubnets(@RequestBody ListSubnetsRequestDTO listSubnetsRequestDTO) {
        ListSubnetsResponse listSubnetsResponse = cloudServiceService.listSubnets(listSubnetsRequestDTO);
        if (listSubnetsResponse == null){
            return error("查询子网列表失败");
        }
        return success(listSubnetsResponse);
    }
    /**
     * https://support.huaweicloud.com/api-vpc/vpc_subnet01_0001.html
     * 创建子网
     * @param regionIdDTO 请求参数
     * @return
     */
    @PostMapping(value = "/createSubnet")
    public AjaxResult createSubnet(@RequestBody RegionIdDTO<CreateSubnetRequest> regionIdDTO) {
        CreateSubnetResponse response = cloudServiceService.createSubnet(regionIdDTO);
        if (response == null){
            return error("创建子网失败");
        }
        return success(response);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0901.html
     * 查询任务的执行状态
     * @param jobId 请求参数
     * @param regionId 请求参数
     * @return
     */
    @GetMapping(value = "/showJob")
    public AjaxResult showJob(String jobId, String regionId) {
        ShowJobResponse showJobResponse = cloudServiceService.showJob(jobId, regionId);
        if (showJobResponse == null){
            return error("查询任务的执行状态失败");
        }
        return success(showJobResponse);
    }

    /**
     * 查询按需产品价格
     * @param rateOnDemandReq 请求参数
     * @return
     */
    @PostMapping("/listOnDemandResourceRatings")
    public AjaxResult listOnDemandResourceRatings(@RequestBody RateOnDemandReqDTO rateOnDemandReq) {
        ListOnDemandResourceRatingsResponse response = cloudServiceService.listOnDemandResourceRatings(rateOnDemandReq);
        if (response == null){
            return error("查询按需产品价格失败");
        }
        return success(response);
    }

    /**
     * 购买服务器（付款 支付宝/微信充值的余额，余额购买）
     * @return
     */
    @PostMapping("/buyServer")
    public AjaxResult buyServer(@RequestBody BuyServerDto buyServerDto) {
        return cloudServiceService.buyServer(buyServerDto);
    }

    /**
     * 购买服务器（对公账号购买）
     * @param ids 购物车数组
     * @param filePath 支付凭证信息
     * @return
     */
    @PostMapping("/buyServerByCorporate")
    public AjaxResult buyServerByCorporate(List<String> ids, String filePath) {
        return cloudServiceService.buyServerByCorporate(ids, filePath);
    }

    /**
     * 查询指定条件下的项目列表
     * @return 项目列表
     */
    @PostMapping("/keystoneListProjects")
    public AjaxResult keystoneListProjects() {
        KeystoneListProjectsResponse keystoneListProjectsResponse = cloudServiceService.keystoneListProjects();
        if (keystoneListProjectsResponse == null){
            return error("查询指定条件下的项目列表失败");
        }
        return success(keystoneListProjectsResponse);
    }

    /**
     * 获取用户信息
     * @return 获取用户信息
     */
    @GetMapping("/keystoneCreateUserTokenByPassword")
    public AjaxResult keystoneCreateUserTokenByPassword() {
        String token = cloudServiceService.keystoneCreateUserTokenByPassword();
        return success(token);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0402.html
     * 查询云服务器规格变更支持列表
     * @return 查询云服务器规格变更支持列表
     */
    @PostMapping("/listResizeFlavors")
    public AjaxResult listResizeFlavors(@RequestBody ListResizeFlavorsRequestDTO listResizeFlavorsRequestDTO) {
        return cloudServiceService.listResizeFlavors(listResizeFlavorsRequestDTO);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0209.html
     * 变更云服务器规格
     * @return 变更云服务器规格
     */
    @PostMapping("/resizeServer")
    public AjaxResult resizeServer(@RequestBody ResizeServerRequestDTO resizeServerRequestDTO) {
        return cloudServiceService.resizeServer(resizeServerRequestDTO);
    }

    /**
     * https://support.huaweicloud.com/api-iam/iam_07_0001.html
     * 查询IAM用户可以访问的帐号详情
     * @return 变更云服务器规格
     */
    @GetMapping("/keystoneListAuthDomains")
    public AjaxResult keystoneListAuthDomains() {
        return success(cloudServiceService.keystoneListAuthDomains());
    }

    /**
     * https://support.huaweicloud.com/api-eip/ListPublicipsV3.html
     * 全量查询弹性公网IP列表
     * @return 全量查询弹性公网IP列表
     */
    @PostMapping("/listPublicips")
    public AjaxResult listPublicips(@RequestBody RegionIdDTO<ListPublicipsRequest> regionIdDTO) {
        return success(cloudServiceService.listPublicips(regionIdDTO));
    }

    /**
     * https://support.huaweicloud.com/api-oce/api_order_00023.html
     * 取消包年/包月资源自动续费
     * @return 取消包年/包月资源自动续费
     */
    @PostMapping("/cancelAutoRenewalResources")
    public AjaxResult cancelAutoRenewalResources(@RequestBody CancelAutoRenewalResourcesRequest cancelAutoRenewalResourcesRequest) {
        return success(cloudServiceService.cancelAutoRenewalResources(cancelAutoRenewalResourcesRequest));
    }

    /**
     * https://support.huaweicloud.com/api-oce/api_order_00021.html
     * 查询客户包年/包月资源列表
     * @return 查询客户包年/包月资源列表
     */
    @PostMapping("/listPayPerUseCustomerResources")
    public AjaxResult listPayPerUseCustomerResources(@RequestBody ListPayPerUseCustomerResourcesRequest listPayPerUseCustomerResourcesRequest) {
        ListPayPerUseCustomerResourcesResponse response = cloudServiceService.listPayPerUseCustomerResources(listPayPerUseCustomerResourcesRequest);
        if (response == null || response.getData() == null || response.getData().size() == 0){
            return error("查询客户包年/包月资源列表失败");
        }
        return success(response);
    }

    /**
     * https://support.huaweicloud.com/api-ecs/ecs_02_0103.html
     * 删除云服务器
     * @return 删除云服务器
     */
    @PostMapping("/deleteServers")
    public AjaxResult deleteServers(@RequestBody RegionIdDTO<DeleteServersRequest> regionIdDTO) {
        DeleteServersResponse response = cloudServiceService.deleteServers(regionIdDTO);
        if (null == response){
            return error("删除云服务器失败");
        }
        return success(response);
    }

}
